<template>
  <div>
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link" style="display: flex;align-items: center;cursor: pointer;overflow: hidden;">
        <el-avatar shape="square" :size="24" class="el-icon--left"
          src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
        {{ props.username }}
        <el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item v-for="(item, i) in props.data" :command="item.path">
            <el-icon>
              <component :is="item.icon" />
            </el-icon>
            {{ item.name }}
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';
const router = useRouter();// 获取路由实例
const props = defineProps({
  // item项的数据
  data: {
    type: Array,
    required: true,
    default: () => [
      {
        name: { type: String, required: true },
        icon: { type: String, required: true },
        path: { type: String, required: true }
      }
    ]
  },
  // 用户名头像
  username: {
    type: String,
    default: JSON.parse(sessionStorage.getItem('userInfo')).username || '用户名为空'
  }
})
const handleCommand = (command) => {
  if (command === '/login') {
    window.sessionStorage.clear();
    router.push(command);//跳转到相应的页面
  } else {
    router.push(command);//跳转到相应的页面
  }
}
</script>
